package com.gitee.wsl.io.channel

import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.Job

interface ChannelJob {
    val job: Job
}

suspend fun ChannelJob.join() {
    job.join()
}

val ChannelJob.isCompleted: Boolean get() = job.isCompleted

val ChannelJob.isCancelled: Boolean get() = job.isCancelled

@OptIn(InternalCoroutinesApi::class)
fun ChannelJob.getCancellationException(): CancellationException = job.getCancellationException()

fun ChannelJob.invokeOnCompletion(block: () -> Unit) {
    job.invokeOnCompletion { block() }
}

fun ChannelJob.cancel(): Unit = job.cancel()